﻿using System;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using DistributeTransaction.DAL;
using log4net;


namespace DistributeTransaction.Controllers
{
    public class OrderController : BaseController
    {
        private ILog log = LogManager.GetLogger(typeof(OrderController));
        private OrderRepository orderRepository;

        public OrderController()
        {
            orderRepository = new OrderRepository(DBContext);
        }

        //
        // GET: /Orders/
        public ActionResult Index()
        {
            return View(orderRepository.GetOrders(CustomerId));
        }
     
        /// <summary>
        /// display orders
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        public ActionResult Detail(int orderId, string sortOrder)
        {
            ViewBag.OrderId = orderId;
            string sortName  = "ProductName";
            if (!string.IsNullOrEmpty(sortOrder))
            {
                string[] array = sortOrder.Split('|');
                sortName = array[0];
                if (array.Length > 1)
                {
                    ViewBag.SortParm = array[1];
                }

                if (array.Length > 2)
                {
                    ViewBag.SortDir = array[2];
                }

                if (array[0] == array[1])
                {
                    if (string.IsNullOrEmpty(ViewBag.SortDir))
                    {
                        ViewBag.SortDir = "asc";
                    }
                    else if (ViewBag.SortDir == "asc")
                    {
                        ViewBag.SortDir = "desc";
                    }
                    else
                    {
                        ViewBag.SortDir = "asc";
                    }
                }
                else
                {
                    ViewBag.SortParm = sortName;
                    if (string.IsNullOrEmpty(ViewBag.SortDir))
                    {
                        ViewBag.SortDir = "asc";
                    }
                }
            }

            var summary = orderRepository.GetOrderSummaryByID(orderId);
            var orderDetails = summary.details;
            
            if (orderDetails != null)
            {
                ViewBag.CanCancel = orderDetails.Select(item => item.Status).FirstOrDefault();
                ViewBag.TotalAmount = string.Format(
                   CultureInfo.CurrentUICulture,
                   "{0:c}",
                   orderDetails.Sum(item => item.SubTotal));

                switch (sortName)
                {
                    case "ProductName":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.details = orderDetails.OrderBy(s => s.ProductName);
                        }
                        else
                        {
                            summary.details = orderDetails.OrderByDescending(s => s.ProductName);
                        }
                        break;
                    case "Price":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.details = orderDetails.OrderBy(s => s.UnitPrice);
                        }
                        else
                        {
                            summary.details = orderDetails.OrderByDescending(s => s.UnitPrice);
                        }
                        break;
                    case "Quantity":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.details = orderDetails.OrderBy(s => s.Quantity);
                        }
                        else
                        {
                            summary.details = orderDetails.OrderByDescending(s => s.Quantity);
                        }
                        break;
                    case "Coupon":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.details = orderDetails.OrderBy(s => s.Coupon);
                        }
                        else
                        {
                            summary.details = orderDetails.OrderByDescending(s => s.Coupon);
                        }
                        break;
                    case "Total":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.details = orderDetails.OrderBy(s => s.SubTotal);
                        }
                        else
                        {
                            summary.details = orderDetails.OrderByDescending(s => s.SubTotal);
                        }
                        break;
                    case "HoldName":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.Payments = summary.Payments.OrderBy(s => s.HoldName);
                        }
                        else
                        {
                            summary.Payments = summary.Payments.OrderByDescending(s => s.HoldName);
                        }
                        break;
                    case "CardNumber":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.Payments = summary.Payments.OrderBy(s => s.CardNumber);
                        }
                        else
                        {
                            summary.Payments = summary.Payments.OrderByDescending(s => s.CardNumber);
                        }
                        break;
                    case "Amount":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.Payments = summary.Payments.OrderBy(s => s.Amount);
                        }
                        else
                        {
                            summary.Payments = summary.Payments.OrderByDescending(s => s.Amount);
                        }
                        break;
                    case "CardType":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.Payments = summary.Payments.OrderBy(s => s.CardType);
                        }
                        else
                        {
                            summary.Payments = summary.Payments.OrderByDescending(s => s.CardType);
                        }
                        break;
                    case "Status":
                        if (ViewBag.SortDir == "asc")
                        {
                            summary.Payments = summary.Payments.OrderBy(s => s.Status);
                        }
                        else
                        {
                            summary.Payments = summary.Payments.OrderByDescending(s => s.Status);
                        }
                        break;
                    default:
                        break;
                }

               
            }

            
            return View(summary);
        }

        [HttpPost]
        public ActionResult CancelOrder(int orderId)
        {
            orderRepository.CancelOrder(orderId);

            return RedirectToAction("Index", "Order");
        }
    }
}
